home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Demos / Extend 3.0 Demo / Demo Libraries / Demo Generic Lib / Demo Generic Lib.rsrc / MODL_12891_Input Data < prev    next >
Encoding:
Text File  |  1994-06-22  |  7.5 KB  |  331 lines

  1. real     dataonly[][2];
  2. real     xdata[],ydata[];
  3. real     dataMax, dataMin;    ** corrected for roundoff error
  4. integer maxSize;        ** maximum size of data table (see createBlock msg)
  5. integer max;            ** length of data array
  6. integer x,y;
  7. integer xPoint;            ** for incrementing x index on CurrentTime input
  8. real     tempx,tempy,xvalue,yvalue;
  9. double    timeArray[];
  10.  
  11. **    This block generates a curve of data over time.
  12. **    Copyright © 1989-1994 by Imagine That, Inc.
  13. **    All Rights Reserved.
  14. ** Extend Generic Library, Input Data block:Alfy Riddle 4/10/89
  15. **        modified     1/1/92  JSL modified for V2.0
  16. **                    8/27/92 JSL removed on yOut
  17. **                    10/6/93 JSL changed default to stepped
  18. **                    2/14/94 DJK modified trace and report
  19. **                    3/10/94 DJK formatted report
  20. **                    3/10/94 DJK added block label to report & trace
  21. **
  22. **  The Sort function of this block came from the:
  23. ** Nonlinear Function Generator 2/20/89
  24. ** Authors: Cheryl Blanford and Chuck Oman
  25. ** MIT Man Vehicle Laboratory
  26. ** Rm 37-219, Cambridge, MA 02139
  27.  
  28. procedure calc()
  29. {
  30.     integer lastX, notDone;
  31.     lastX = 0;        ** this is a starting point for the interpolation
  32.                     ** it keeps track of the last interpolated point
  33.     
  34.     **
  35.     **    dataMax & dataMin are calculated on initSim 
  36.     **  read in x value and interpolate to find the y value.
  37.     **    'max' gives the number of data points & is set during initSim
  38.     **
  39.     
  40.     if( repeat )
  41.         {
  42.         if( currentTime >= repTime )
  43.             xPoint = -1;        ** reset this value
  44.             
  45.         xValue = realMod(currentTime,repTime);
  46.         }
  47.     else
  48.         xValue = currentTime;
  49.  
  50.     ** get current valid range of data (xPoint = -1 in initSim)
  51.     while( (xValue*1.00000000001 >= data[xPoint+1][0]) && (xPoint+1 < max) ) 
  52.         {
  53.         xPoint++;
  54.         }
  55.  
  56.     if( xPoint+1 >= max )    ** data at last point or too high
  57.     {
  58.         if( (xPoint == max-1) && (realAbs(xValue-data[xPoint][0]) < 1.0e-15) )
  59.             yOut = data[xPoint][1];
  60.         else                ** ERROR - above range
  61.         {
  62.             if( stop )
  63.             {
  64.                  userError
  65.                  ("Simulation time value has exceeded highest time value in block number "+(MyBlockNumber()));
  66.                   abort;
  67.             }
  68.             else
  69.                 yOut = 0.0;
  70.         }
  71.     }
  72.     else                    ** data in or below range
  73.     {
  74.         if( (xPoint >= 0) && (xPoint < max-1) )
  75.         {
  76.             if( interp )        ** interpolate
  77.             {
  78.                   yValue = ((xValue - data[xPoint][0])*(data[xPoint+1][1] - 
  79.                       data[xPoint][1])/(data[xPoint+1][0] - data[xPoint][0])) 
  80.                       + data[xPoint][1];
  81.               }
  82.               else                **    stepped
  83.               {
  84.                   yValue = data[xPoint][1];
  85.               }
  86.  
  87.                yOut = yValue;
  88.         }
  89.         else                ** data near first point or ERROR
  90.         {
  91.             if( (realAbs(xValue-data[0][0]) < 1.0e-15) )
  92.                 yOut = data[0][1];
  93.             else                ** ERROR - below range
  94.             {
  95.                 if( stop )
  96.                 {
  97.                      userError
  98.                      ("Simulation time value is below lowest time value in block number "+(MyBlockNumber()));
  99.                       abort;
  100.                 }
  101.                 else
  102.                     yOut = 0.0;
  103.             }
  104.         }
  105.     }
  106.  
  107.     ** sysGlobal2 is the file reference number for the DEBUG TRACE
  108.     if( sysGlobal2 != 0.0 ) ** 0 is error, check for open file for TRACE
  109.         {
  110. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  111.         fileWrite(sysGlobal2,"Input Data                   block number "+(MyBlockNumber())+".  Current Time:"+currentTime+".","",True);
  112.         if(getBlockLabel(myBlockNumber()) != "")
  113.             fileWrite(sysGlobal2,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  114.         fileWrite(sysGlobal2,"     Time = "+currentTime,"",True);
  115.         fileWrite(sysGlobal2,"     Output = "+yOut,"",True);
  116.         fileWrite(sysGlobal2," ","",True);
  117.         }
  118. }
  119.  
  120.  
  121. Procedure validMax()
  122. {
  123.     integer i;
  124.     i=0;
  125.     
  126.     while( !noValue(data[i][0]) && i<maxSize )
  127.         i++;
  128.     max = i;
  129.  
  130.     if (max <= 1)    ** check for at least 2 rows
  131.         {
  132.         usererror("Must have at least two rows of data in Input Data block number "+(MyBlockNumber()));
  133.         abort;
  134.         }
  135. }
  136.  
  137.  
  138. Procedure sortFcn()
  139. {
  140.     validMax();
  141.     
  142.     ** split data into 2 arrays for ease of sorting.    
  143.     makearray(dataonly,max);
  144.     makearray(xdata,max);
  145.     makearray(ydata,max);                
  146.  
  147.     for (x=0;x<max;x++)
  148.       {
  149.           dataonly[x][0] = data[x][0];
  150.            dataonly[x][1] = data[x][1];
  151.        }
  152.  
  153.     for(x = 0 ;x<max;x++)
  154.      {
  155.          xdata[x] = data[x][0];
  156.           ydata[x] = data[x][1];
  157.       }
  158.     
  159.     for (x=0;x<max;x++)
  160.           for (y=x+1;y<max;y++)
  161.             if(xdata[y] < xdata[x])
  162.             {
  163.                  tempx = xdata[y];
  164.                  tempy = ydata[y];
  165.                  xdata[y] = xdata[x];
  166.                  ydata[y] = ydata[x];
  167.                  xdata[x] = tempx;
  168.                  ydata[x] = tempy;
  169.             }
  170.  
  171.  
  172.     **restore table, now properly sorted.
  173.     for (x = 0;x<max;x++ )
  174.     {
  175.         data[x][0] = xdata[x];
  176.           data[x][1] = ydata[x];
  177.       }
  178. }
  179.  
  180.  
  181. on choosetoplot
  182. {
  183.     integer k, numPlot;
  184.     validMax();
  185.     
  186.             ** support stepped plots
  187.     if( interp )
  188.         numPlot = max;
  189.     else if( stepped )
  190.         numPlot = 2 * max - 1;
  191.  
  192.     makearray(xdata,numPlot);
  193.     makearray(ydata,numPlot);                
  194.     
  195.     for (k=0;k<max;k++)
  196.     {
  197.         if( interp )
  198.         {
  199.             xdata[k] = data[k][0];
  200.             ydata[k] = data[k][1];
  201.         }
  202.         else    ** stepped
  203.         {
  204.             xdata[2*k] = data[k][0];
  205.             ydata[2*k] = data[k][1];
  206.             
  207.             if( k < max-1 )    ** odd number of points (skip last)
  208.             {
  209.                 xdata[2*k+1] = data[k+1][0];    ** x coordinates
  210.                 ydata[2*k+1] = data[k][1];
  211.             }
  212.         }
  213.     }
  214.     
  215.     ** install the axes
  216.     installAxis(0, "Input Data",
  217.          "Time", FALSE, 0,20,
  218.             "Y Output", FALSE, 0,20, "", 0, 0, 0, 
  219.             blackpattern, blackcolor, 100);
  220.     installArray(0, 0, "Time", xdata, 0, 20, 
  221.                     0, 0, blackPattern, cyanColor);
  222.     installArray(0, 1, "Y Output", ydata, 0, 20,
  223.                     0, 0, dkgrayPattern, redColor);
  224.  
  225.     makeScatter(0, 0);    
  226.     ** plot the data
  227.  
  228.       for (k=0;k<numPlot;k++)
  229.           plotNewScatter(0,0,k,xdata[k],ydata[k]);
  230.     
  231.     autoscalex(0);
  232.     autoscaley(0);
  233.     showPlot(0, "Input Data");
  234. }        
  235.  
  236.  
  237.  
  238. ** This message occurs for each step in the simulation.
  239. on simulate
  240. {
  241.     calc();
  242. }
  243.  
  244.  
  245. on endsim
  246. {
  247.     integer i;
  248.     
  249.     ** sysGlobal1 is the file reference number for the TEXT REPORT
  250.     if( sysGlobal1 != 0.0 ) ** 0 is error, check for open file for REPORT
  251.         {
  252. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  253.         fileWrite(sysGlobal1,"Input Data                   block number "+MyBlockNumber(),"",True);
  254.         if(getBlockLabel(myBlockNumber()) != "")
  255.             fileWrite(sysGlobal1,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  256.         if( interp )
  257.             fileWrite(sysGlobal1,"     Interpolated","",True);
  258.         else
  259.             fileWrite(sysGlobal1,"     Stepped","",True);
  260.             
  261.         if( stop )
  262.             fileWrite(sysGlobal1,"     Stops when out of range","",True);
  263.         else
  264.             fileWrite(sysGlobal1,"     Sets output to zero when out of range","",True);
  265.  
  266.         if( repeat )
  267.             fileWrite(sysGlobal1,"     Repeats every "+repTime+" time units","",True);
  268.  
  269.         fileWrite(sysGlobal1,"     Table time     y","",TRUE);
  270.         
  271.         validMax();
  272.         
  273.         for( i=0;i<max;i++ )    ** picks up old itemNum
  274.             fileWrite(sysGlobal1,"     "+RealToStr(data[i][0],8)+"      "+RealToStr(data[i][1],8),"",TRUE);
  275.  
  276. //        for( i=0;i<max;i++ )    ** picks up old itemNum
  277. //            fileWrite(sysGlobal1,"     "+data[i][0]+", "+data[i][1],"",TRUE);
  278.         
  279.         fileWrite(sysGlobal1," ","",True);
  280.         }
  281. }
  282.  
  283. on createBlock
  284. {
  285.     maxSize    = 500;
  286.     interp    = 0;
  287.     stepped    = 1;
  288.     stop    = 1;
  289.     zero    = 0;
  290.     repeat    = 0;
  291.     repTime = 10;
  292. }
  293.   
  294. on checkdata
  295. {
  296.     sysGlobal1 = 0.0;    ** prevent false reports
  297.     sysGlobal2 = 0.0;    **  prevent false debugs
  298.  
  299.     validMax();        ** check for at least 2 rows
  300. }
  301.  
  302.  
  303. ** Initialize any simulation variables.
  304. on initsim
  305. {
  306.     sortFcn();        ** sort
  307.  
  308.     if( data[max-1][0] > 0 )        ** for roundoff error
  309.         dataMax = data[max-1][0]*1.000000001;
  310.     else
  311.         dataMax = data[max-1][0]*0.999999999;
  312.  
  313.     if( data[0][0] > 0 )
  314.         dataMin = data[0][0]*0.999999999;
  315.     else
  316.         dataMin = data[0][0]*1.000000001;
  317.         
  318.     xPoint = -1;    ** for currentTime mode
  319.     
  320.     if( repeat )
  321.         zero = 1;    ** do not abort when data out of range
  322.  
  323.     if( getPassedArray(sysGlobal0, timeArray) )
  324.         getSimulateMsgs(FALSE);
  325. }
  326.  
  327.  
  328. on sort
  329. {
  330.     sortFcn();    ** then sort it
  331. }